import { blockMod } from '@/utils/data'


// 获取下一个方块数据
const getNextBlock = (last) => {
    let data = blockMod[Math.floor(Math.random() * blockMod.length)]
    let angle = Math.floor(Math.random() * data.transition.length)
    let transData = data.transition[angle]
    let block = []
    for (let i = 0; i < transData.length; i++) {
        block.push(data.site[i] + transData[i])
    }
    console.log("angle:", angle, "block:", block, "transData:", transData)
    return {
        site: data.site,
        color: data.color,
        transition: data.transition,
        block: block,
        angle: angle,
        lastBlock: last
    }
}

function originalBlock() {
    return {
        data: 0,
        bg: ''
    }
}

// 渲染方块
const renderBlock = (frame, block, color) => {
    if (!block || block.length == undefined) return
    for (let i = 0; i < block.length; i += 2) {
        frame[block[i]][block[i + 1]].bg = color
    }

}

// 清除方块
const clearBlock = (frame, block) => {
    if (!block || block.length == undefined) return
    for (let i = 0; i < block.length; i += 2) {
        frame[block[i]][block[i + 1]] = originalBlock()
    }
}

// 判断方块位置是否被占用
const isBlockOccupied = (frame, block) => {
    for (let i = 0; i < block.length; i += 2) {
        if (frame[block[i]][block[i + 1]].data == 1) return true
    }
    return false
}

//  判断方块是否超出主屏幕边界
const isOutOfBounds = (block, row, col) => {
    for (let i = 0; i < block.length; i += 2) {
        if (block[i] < 0 || block[i] >= row) return true
        if (block[i + 1] < 0 || block[i + 1] >= col) return true
    }
    return false
}

const calculateSite = (origin, transData, row, col) => {
    let newSite = []
    for (let i = 0; i < transData.length; i += 2) {
        newSite[i] = origin[i] + transData[i] + row
        newSite[i + 1] = origin[i + 1] + transData[i + 1] + col
    }
    return newSite
}

export {
    getNextBlock,
    originalBlock,
    renderBlock,
    clearBlock,
    isBlockOccupied,
    isOutOfBounds,
    calculateSite
}